<template>
  <div class="container">
    <div class="search-box">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名称">
          <el-input
            v-model="searchForm.username"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="帖子类型">
          <el-input
            v-model="searchForm.type"
            placeholder="请输入帖子类型"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-box clearfix">
      <div class="button-box clearfix">
        <el-button type="danger" @click="handleDeleteList">批量删除</el-button>
      </div>
      <el-table
        border
        :data="postList"
        style="width: 100%; min-height: 400px"
        @selection-change="handleSelectionChange"
        height="400"
      >
        <el-table-column
          show-overflow-tooltip
          align="center"
          type="selection"
          width="55"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="id"
          label="帖子ID"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="username"
          label="用户名称"
          width="180"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="user_id"
          label="用户ID"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="title"
          label="帖子标题"
          width="200"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="content"
          label="帖子内容"
          width="300"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="type"
          label="帖子类型"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="type_id"
          label="评论主体ID"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="favorite_count"
          label="收藏数"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="comment_count"
          label="评论数"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="created_time"
          label="创建时间"
          width="180"
        />
        <el-table-column
          label="操作"
          align="center"
          show-overflow-tooltip
          width="250"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button size="mini" @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 查看对话框 -->
    <el-dialog
      :title="`查看帖子 - ${selectedPost.title}`"
      :visible.sync="dialogVisible"
      width="80%"
      :before-close="handleDialogClose"
      style="overflow-y: auto"
    >
      <div v-html="selectedPost.content"></div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleDialogClose">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "PostManagement",

  data() {
    return {
      searchForm: {
        username: "",
        type: "",
      },
      postList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      selectedPost: {},
      multipleSelection: [],
    };
  },
  computed: {
    ...mapGetters(["tableColumnLabels"]),
  },
  created() {
    this.fetchPostList();
  },
  methods: {
    fetchPostList() {
      // 模拟异步请求
      let res = {
        data: [
          {
            id: 1,
            username: "张三",
            user_id: 1001,
            title: "示例帖子",
            content: "<p>这是一个示例帖子的内容。</p>",
            type: "技术分享",
            type_id: 2001,
            favorite_count: 50,
            comment_count: 20,
            created_time: "2025-06-01 10:00:00",
          },
        ],
      };
      this.postList = res.data;
      this.total = res.data.length;
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchPostList();
    },
    resetSearch() {
      this.searchForm = {
        username: "",
        type: "",
      };
      this.fetchPostList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchPostList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchPostList();
    },
    handleView(row) {
      this.selectedPost = { ...row };
      this.dialogVisible = true;
    },
    handleDelete(row) {
      // 删除逻辑
      // 模拟删除
      this.$confirm("确定要删除该帖子吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$message({
          type: "success",
          message: "帖子删除成功！",
        });
        this.fetchPostList();
      });
    },
    handleDialogClose() {
      this.dialogVisible = false;
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    handleDeleteList() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择需要删除的帖子");
        return;
      }
      this.$confirm("确定要删除选中的帖子吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const ids = this.multipleSelection.map((item) => item.id);
          console.log("批量删除帖子 ID:", ids);
          this.$message.success("批量删除成功");
          this.fetchPostList();
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}

.search-box {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  align-items: center;
}

.table-box {
  .el-table {
    margin-bottom: 20px;
  }
}

.product-image {
  border-radius: 4px;
}
</style>
